'''
Author: @zhyy
Date: 2021-01-27 14:23:31
LastEditors: @zhyy
LastEditTime: 2021-01-31 14:35:50
'''

import time
from sqlalchemy import Column,String,Integer,Text,DateTime,ForeignKey,Enum
from sqlalchemy.orm import relationship
from werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from app import db



class BaseDB():
    id = Column(Integer, primary_key=True)  # 编号
    create_time = Column(DateTime, index=True, default=time.strftime('%Y-%m-%d %H:%M:%S' , time.localtime(time.time())))  # 添加时间
    update_time = Column(DateTime, index=True, default=None)  # 更新时间
    delete_time = Column(DateTime, index=True, default=None)  # 删除时间

# 文章
class Article(db.Model,BaseDB):
    __tablename__ = "articles"
    title = Column(String(255), unique=True)  # 标题
    content= Column(Text)  # 文章内容
    user_id = Column(Integer, ForeignKey('users.id'))  # 所属用户
    article_sort_id = Column(Integer, ForeignKey('article_sort.id'))  # 所属文章分类
    comment= relationship('ArticleComment', backref='articles')  #文章评论
    def __repr__(self):
        return "<Article %r>" % self.title



# 文章评论
class ArticleComment(db.Model,BaseDB):
    __tablename__ = "article_comments"
    comment= Column(Text)  # 评论内容
    nick_name=Column(String(50),default="游客")  # 昵称
    email=Column(String(255),default="")  # 邮箱
    face_img= Column(String(255), default="")  # 头像
    article_id = Column(Integer, ForeignKey('articles.id'))  # 所属文章
    reply_comment = relationship('ReplyComment', backref='article_comments')  # 文章评论
    def __repr__(self):
        return "<ArticleCommen %r>" % self.id

# 文章评论回复表
class ReplyComment(db.Model,BaseDB):
    __tablename__ = "reply_comments"
    reply_comment = Column(Text)  # 评论内容
    comment_id = Column(Integer, ForeignKey('article_comments.id'))  # 所属评论

    def __repr__(self):
        return "<ReplyComment %r>" % self.id

#文章类型表
class ArticleSort(db.Model,BaseDB):
    __tablename__ = "article_sort"
    sort = Column(String(50),unique=True,default="编程类")  # 分类
    article= relationship('Article', backref='article_sort')  #文章
                
    def __repr__(self):
        return "<ArticlSort %r>" % self.sort

# 用户
class User(db.Model,BaseDB):
    __tablename__ = "users"
    name = Column(String(50), unique=True)  # 昵称
    password_hash = Column(String(128))  # 密码hash值
    email = Column(String(50), unique=True)  # 邮箱
    phone = Column(String(50), unique=True)  # 手机号码
    articles = relationship('Article', backref='users')  # 文章外键
    goods = relationship('Goods', backref='users')  # 商品外键
    order= relationship('Order', backref='users')  # 订单外键
    role_id = Column(Integer, ForeignKey('roles.id'))  # 用户角色
    
    def __repr__(self):
        return "<User %r>" % self.name

    #通过property装饰器把函数设置为属性
    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    # 生成密码哈希值，保存到数据库password_hsah变量中
    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    # 验证密码是否正确，返回True和False
    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

# 角色
class Role(db.Model,BaseDB):
    __tablename__ = "roles"
    role = Column(String(50), unique=True)  # 昵称
    user= relationship('User', backref='roles')  #文章评论
    
    def __repr__(self):
        return "<Role %r>" % self.role

 # 会员登录日志
class Userlog(db.Model,BaseDB):
    __tablename__ = "userlog"
    user_id = Column(Integer, ForeignKey('users.id'))  # 所属会员编号
    ip = Column(String(100))  # 登录IP
   

    def __init__(self, ip):
        self.ip = ip

    def __repr__(self):
        return "<Userlog %r>" % self.id

# 操作日志
class Oplog(db.Model,BaseDB):
    __tablename__ = "oplog"
    user_id = Column(Integer, ForeignKey('users.id'))  # 所属管理员编号
    ip = Column(String(100))  # 登录IP
    reason = Column(String(600))  # 操作原因
   
    def __repr__(self):
        return "<Oplog %r>" % self.id

# 商品
class Goods(db.Model,BaseDB):
    __tablename__ = "goods"
    name = Column(String(255), unique=True)  # 标题
    danjia = Column(String(50))  # 单价
    kucun = Column(Integer)  # 库存
    sell_num= Column(Integer) # 销量
    miaoshu = Column(Text)  # 商品描述
    goods_sort_id = Column(Integer, ForeignKey('goods_sort.id'))  # 所属商品 分类
    goods_img = relationship('GoodsImg', backref='goods')  #文章图片
    orders= relationship('Order', backref='goods')  #商品订单
    user_id = Column(Integer, ForeignKey('users.id'))  # 所属用户
    
    def __repr__(self):
        return "<Goods %r>" % self.name


#商品类型表
class GoodsSort(db.Model,BaseDB):
    __tablename__ = "goods_sort"
    sort = Column(String(50),unique=True,default="")  # 分类
    goods= relationship('Goods', backref='goods_sort')  #商品

    def __repr__(self):
        return "<GoodsSort %r>" % self.sort

#座位表
class Tables(db.Model,BaseDB):
    __tablename__ = "tables"

    sort = Column(String(50),unique=True,default="")  # 分类
    table=Column(String(50),unique=True,default="")  # 座位号
    orders= relationship('Order', backref='tables')  #商品订单
   
    def __repr__(self):
        return "<Tables %r>" % self.table



#商品图片
class GoodsImg(db.Model,BaseDB):
    __tablename__ = "goods_imgs"
    img = Column(String(255))  # 标题
    goods_id = Column(Integer, ForeignKey('goods.id'))  # 所属商品
   



# 订单
class Order(db.Model,BaseDB):
    __tablename__ = "orders"
    order_time=Column(String(50),default="")
    member=Column(Integer,default="")
    num = Column(Text)  # 数量
    order_number=Column(String(50)) # 订单编号
    pay_sort=Column(Integer) # 支付类型
    phone=Column(String(255),default="")  # 查询手机
    status=Column(Enum(
        "未付款","已付款","已发货"
    ),default="未付款",nullable=False) # 支付状态
    goods_id = Column(Integer, ForeignKey('goods.id'))  # 所属商品
    table_id=Column(Integer, ForeignKey('tables.id'))  # 预定座位号id
    user_id = Column(Integer, ForeignKey('users.id'))  # 所属用户
   
    def __repr__(self):
        return "<Order %r>" % self.id


        








